/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'article_list', params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>文章管理</h3>
            <h5>网站系统文章添加与编辑</h5>
          </div>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="标题" required>
          <el-input v-model="form.title" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.title && !form.title">{{errResult.title}}</p>
        </el-form-item>
        <el-form-item label="所属分类" required>
          <el-cascader
            clearable
            size="mini"
            :show-all-levels="false"
            v-model="form.cat_id"
            placeholder="选择分类"
            :options="articleCats"
            :props="{checkStrictly: true,emitPath: false, value: 'cat_id', label: 'cat_name'}"
          ></el-cascader>
          <p class="err-msg" v-if="errResult.cat_id && !form.cat_id">{{errResult.cat_id}}</p>
          <p class="notic">当选择发布"商城公告"时，还需要设置下面的"出现位置"项</p>
        </el-form-item>
        <el-form-item label="seo关键字">
          <el-input v-model="form.keywords" size="mini"></el-input>
          <p class="notic">用于seo 搜索引擎友好</p>
        </el-form-item>
        <el-form-item label="链接">
          <el-input v-model="form.link" size="mini"></el-input>
          <p class="notic">当填写"链接"后点击文章标题将直接跳转至链接地址，不显示文章内容。链接格式请以http://开头</p>
        </el-form-item>
        <el-form-item label="发布时间">
          <div class="block">
            <el-date-picker
              size="mini"
              v-model="form.publish_time"
              format="yyyy-MM-dd"
              value-format="timestamp"
              type="date"
              placeholder="选择日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="显示">
          <el-switch v-model="form.is_open" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="网页描述">
          <el-input type="textarea" v-model="form.description" style="width:350px" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" required>
          <Ueditor :value="ueditor.value" :config="ueditor.config" ref="ue" v-if="ueditor.isShow"></Ueditor>
          <p
            class="err-msg"
            v-if="errResult.content && $refs.ue.getUEContent() == ''"
          >{{errResult.content}}</p>
        </el-form-item>
        <el-form-item label="图片上传" v-show="form.type != 0">
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=article'"
            :show-file-list="false"
            :on-success="handleUpload"
            :before-upload="beforeAvatarUpload"
          >
            <el-image v-if="form.thumb" :src="form.thumb" style="width: 100px; height: 100px"></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="notic">请上传图片格式文件</p>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit()" :loading="isLoading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  updateArticle,
  getArticle,
  getArticleCats,
  addArticle
} from "@/utils/api";
import Ueditor from "@/components/ueditor/index";
export default {
  data() {
    return {
      article_id: this.$route.params.id,
      form: {},
      articleCats: [],
      ueditor: {
        isShow: false,
        value: "",
        config: {
          serverUrl: this.apiHead + "/mall/ueditor/config?path=article",
          zIndex: 999,
          initialFrameWidth: "80%", //初化宽度
          initialFrameHeight: 300, //初化高度
          focus: false, //初始化时，是否让编辑器获得焦点true或false
          maximumWords: 99999,
          removeFormatAttributes:
            "class,style,lang,width,height,align,hspace,valign", //允许的最大字符数 'fullscreen',
          pasteplain: false, //是否默认为纯文本粘贴。false为不使用纯文本粘贴，true为使用纯文本粘贴
          autoHeightEnabled: true
        }
      },
      errResult: {},
      isLoading: false
    };
  },
  created() {
    this.getArticleCats();
    if (this.$route.params.id) {
      this.getArticle();
    } else {
      this.ueditor.isShow = true;
    }
  },
  components: {
    Ueditor
  },
  methods: {
    getArticle() {
      var that = this;
      getArticle(this.article_id).then(function(res) {
        res.publish_time *= 1000;
        that.ueditor.value = res.content;
        res.is_open = String(res.is_open);
        that.form = res;
        that.ueditor.isShow = true;
      });
    },
    getArticleCats() {
      var that = this;
      getArticleCats().then(function(res) {
        that.articleCats = res;
      });
    },
    submit() {
      this.isLoading = true;
      this.form.publish_time /= 1000;
      this.form.content = this.$refs.ue.getUEContent();
      var that = this;
      if (this.form.article_id) {
        //编辑
        updateArticle(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              onClose: function() {
                that.$router.push({ name: "article_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        addArticle(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              onClose: function() {
                that.$router.push({ name: "article_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    },
    handleUpload(res) {
      this.form.thumb = res.result;
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isPic =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPic) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isPic && isLt2M;
    }
  }
};
</script>

<style scoped>
.avatar-uploader-icon {
  line-height: 100px;
}
>>> .el-input__inner {
  width: 300px;
}
</style>
